package val.to.UI;

import java.io.FileNotFoundException;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;

import val.to.BitmapUtil;
import val.to.Globals;
import val.to.R;
import val.to.SessionEvents;
import val.to.Utility;
import val.to.ValtoActivity;
import val.to.ValtoServices;
import val.to.entities.UploadItem;
import val.to.server.RESTServerPortal;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class PhotoUpload extends ValtoActivity {
	ImageView mImage;
	Uri mImageUri;
	EditText mMessage;
	Button mPostButton;
	Handler mHandler;
	Bitmap mBitmap;
	CheckBox mFacebookCheckBox;
	ProgressDialog mLoadingDialog;
	MenuButton mMenuButton;
	boolean mNoEventFlag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.post_photo);

		mNoEventFlag = false;
		mHandler = new Handler();
		Bundle b = getIntent().getExtras();
		if (b.containsKey(Intent.EXTRA_STREAM)) {
			// Get resource path from intent callee
			mImageUri = (Uri) b.getParcelable(Intent.EXTRA_STREAM);
			mNoEventFlag = true;
			if (Globals.FacebookSession == null)
				Globals.Init(this);
			if (!Globals.FacebookSession.isSessionValid()) {
				Intent intent = new Intent(this, EntranceActivity.class);
				intent.putExtra("quickUpload", "whatever");
				startActivity(intent);
			} else if (ValtoServices.getCurrentEvent() == null) {
				Intent intent = new Intent(this, EventsActivity.class);
				intent.putExtra("quickUpload", "whatever");
				startActivity(intent);
			}
		} else if (b.containsKey("photoUrl")) {
			mImageUri = (Uri) b.get("photoUrl");
		}

		mImage = (ImageView) findViewById(R.id.PostedPhoto);

		mMenuButton = (MenuButton) findViewById(R.id.menuButton);
		mMenuButton.init(this);

		mFacebookCheckBox = (CheckBox) findViewById(R.id.FacebookCheckbox);

		try {
			int screenWidth = Utility.getScreenWidth(this);
			mBitmap = BitmapUtil.loadBitmap(mImageUri, this, screenWidth,
					screenWidth, true, true);
			mImage.setImageBitmap(mBitmap);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		mMessage = (EditText) findViewById(R.id.MessageEditBox);
		mPostButton = (Button) findViewById(R.id.PostButton);
		mPostButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mImage.setImageDrawable(null);
				mBitmap.recycle();

				for (int i = 0; i < 1; i++) {
					Globals.UploadManager
							.Upload(PhotoUpload.this, new UploadItem(mImageUri,
									mMessage.getText().toString(),
									mFacebookCheckBox.isChecked()));
				}

				Toast.makeText(PhotoUpload.this,
						"Your photo will be uploaded soon", Toast.LENGTH_LONG)
						.show();
				setResult(Activity.RESULT_OK);
				finish();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (ValtoServices.getCurrentEvent() == null) {
			if (mNoEventFlag)
				mNoEventFlag = false;
			else
				finish();
		}
	}
}
